package com.bawei.yuekao;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.RadioGroup;

import com.bawei.yuekao.adapter.WelComeAdapter;
import com.bawei.yuekao.fragment.WelcomeFragmentOne;
import com.bawei.yuekao.fragment.WelcomeFragmentThree;
import com.bawei.yuekao.fragment.WelcomeFragmentTwo;
import com.bawei.yuekao.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class WelComeActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private List<Fragment> list;
    private WelComeAdapter welComeAdapter;
    private RadioGroup radioGroup;
    private Timer timer;
    private int position = 0;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(msg.what == 102){
                viewPager.setCurrentItem(position++);
                if(position == 3){
                    timer.cancel();
                    Intent intent = new Intent(WelComeActivity.this,MainActivity.class);
                    startActivity(intent);
                    //sp记录
                    SPUtils.getInstance(WelComeActivity.this).putString("go","1");


                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wel_come);

        //sp获得值
        String go = SPUtils.getInstance(this).getString("go");
        if(!go.equals("")){
            Intent intent = new Intent(WelComeActivity.this,MainActivity.class);
            startActivity(intent);
            return;//!!!!!!!!结束
        }


        viewPager = findViewById(R.id.view_pager);
        radioGroup = findViewById(R.id.rg);
        list = new ArrayList<>();
        list.add(new WelcomeFragmentOne());
        list.add(new WelcomeFragmentTwo());
        list.add(new WelcomeFragmentThree());
        welComeAdapter = new WelComeAdapter(getSupportFragmentManager(),list);
        viewPager.setAdapter(welComeAdapter);

        //滑动viewpager
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        radioGroup.check(R.id.rb1);
                        break;
                    case 1:
                        radioGroup.check(R.id.rb2);
                        break;
                    case 2:
                        radioGroup.check(R.id.rb3);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //自动滑动
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(102);

            }
        },0,1000);

    }
}
